package com.zhcd.sys.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhcd.sys.pojo.Manage;
import com.zhcd.sys.service.ManageService;
import com.zhcd.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author feifei
 * @since 2021-11-29
 */
@RestController
@RequestMapping("/sys/manage")
public class ManageController {
    //注入ManageService
    @Autowired
    public ManageService manageService;

    //登录
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestBody Map<String,Object> para) throws IOException {
        String username=(String)para.get("username");
        String password=(String)para.get("password");
        Manage manage = manageService.login(username, password);
        HashMap<String,Object> hs=new HashMap<>();
        if (manage!=null) {
            //生成token
            String token= TokenUtil.sign(manage);
            hs.put("token",token);
            hs.put("code","200");
            hs.put("url","/home");
            hs.put("flag","success");
            hs.put("data",manage);
            Date date = new Date();
            manageService.upd_loginTime(manage,date);
        }
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    //注册
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    public String register(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        Manage manage=(Manage)para.get("manage");
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("code","200");
        hs.put("data",manageService.register(manage));
        hs.put("flag","success");
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    //查询全部
    @RequestMapping(value = "/getAll_Manage",method = RequestMethod.GET)
    @ResponseBody
    public String getAll_Manage() throws JsonProcessingException{
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("code","200");
        hs.put("data",manageService.getAll_Manage());
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    //id查询单个用户
    @RequestMapping(value = "/getOne_Manage",method = RequestMethod.GET)
    @ResponseBody
    public String getOne_Manage(@RequestBody Map<String,Object> para) throws JsonProcessingException{
        Integer id=(Integer)para.get("id");
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("code","200");
        hs.put("data",manageService.getOne_Manage(id));
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}

